const express = require('express')
// 创建路由对象
const router = express.Router()

//导入数据库操作模块
const db = require('../db/index')

//获取类别
router.get('/category', (req, res) => {
    const sql = `select category from movieinfo Group by category`
    db.query(sql, function (err, results) {
        if (err) return res.cc(err)
        let arr = []
        for (var i in results) {
            let a = Object.values(results[i])
           arr.push(a[0])
        }
        res.send({
            status:200,
            message:'获取分类成功！',
            data:arr
        })
    })
})


//获取类别内容
router.get('/getCategoryMovie', (req, res) => {
    const sql = `select name,posters,price from movieinfo where category = '${req.query.category}'`
    db.query(sql, function (err, results) {
        if (err) return res.cc(err)
        res.send({
            status:200,
            message:'获取分类成功！',
            data:results
        })
    })
})


// 将路由对象共享出去
module.exports = router